package com.sys.SysAquaticSpecies.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

import com.sys.SysTanks.domain.SysTanks;
import com.sys.SysTanks.mapper.SysTanksMapper;
import com.sys.common.exception.ServiceException;
import com.sys.common.utils.DateUtils;
import com.sys.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sys.SysAquaticSpecies.mapper.SysAquaticSpeciesMapper;
import com.sys.SysAquaticSpecies.domain.SysAquaticSpecies;
import com.sys.SysAquaticSpecies.service.ISysAquaticSpeciesService;

/**
 * 养殖生物信息Service业务层处理
 * 
 * @author sys
 * @date 2025-09-04
 */
@Service
public class SysAquaticSpeciesServiceImpl implements ISysAquaticSpeciesService 
{
    @Autowired
    private SysAquaticSpeciesMapper sysAquaticSpeciesMapper;

    @Autowired
    private SysTanksMapper sysTanksMapper;

    /**
     * 查询养殖生物信息
     * 
     * @param id 养殖生物信息主键
     * @return 养殖生物信息
     */
    @Override
    public SysAquaticSpecies selectSysAquaticSpeciesById(Long id)
    {
        return sysAquaticSpeciesMapper.selectSysAquaticSpeciesById(id);
    }

    /**
     * 查询养殖生物信息列表
     * 
     * @param sysAquaticSpecies 养殖生物信息
     * @return 养殖生物信息
     */
    @Override
    public List<SysAquaticSpecies> selectSysAquaticSpeciesList(SysAquaticSpecies sysAquaticSpecies)
    {

        return sysAquaticSpeciesMapper.selectSysAquaticSpeciesList(sysAquaticSpecies);
    }

    /**
     * 新增养殖生物信息
     * 
     * @param sysAquaticSpecies 养殖生物信息
     * @return 结果
     */
    @Override
    public int insertSysAquaticSpecies(SysAquaticSpecies sysAquaticSpecies)
    {
        return sysAquaticSpeciesMapper.insertSysAquaticSpecies(sysAquaticSpecies);
    }

    /**
     * 修改养殖生物信息
     * 
     * @param sysAquaticSpecies 养殖生物信息
     * @return 结果
     */
    @Override
    public int updateSysAquaticSpecies(SysAquaticSpecies sysAquaticSpecies)
    {
        Date now = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        int updated = 0;

        // ========== 情况1：批量逻辑删除 ==========
        if (sysAquaticSpecies.getIds() != null && !sysAquaticSpecies.getIds().isEmpty()) {
            List<Long> speciesIds = sysAquaticSpecies.getIds();

            // 检查这些物种是否被“正在养殖”的桶使用
            int count = sysTanksMapper.countBySpeciesIdsAndStatus(speciesIds);
            if (count > 0) {
                throw new ServiceException("共存在 " + count + " 个养殖桶正在养殖这些物种，无法删除");
            }

            // 批量更新状态为 0（逻辑删除）
            for (Long speciesId : speciesIds) {
                SysAquaticSpecies species = new SysAquaticSpecies();
                species.setId(speciesId);
                species.setStatus(0L);
                species.setUpdateTime(now);
                species.setUpdatedBy(username);
                updated += sysAquaticSpeciesMapper.updateSysAquaticSpecies(species);
            }
            return updated;
        }

        // ========== 情况2：单个逻辑删除（status = 0）==========
        if (sysAquaticSpecies.getStatus() != null && sysAquaticSpecies.getStatus() == 0) {
            Long id = sysAquaticSpecies.getId();
            if (id == null) {
                throw new IllegalArgumentException("删除操作必须提供 id");
            }

            SysTanks sysTanks = new SysTanks();
            sysTanks.setSpeciesId(id);
            sysTanks.setStatus("空桶");
            int count = sysTanksMapper.countByCondition(sysTanks);

            if (count > 0) {
                throw new ServiceException("存在 " + count + " 个养殖桶养殖该物种，无法删除");
            }
        }

        // ========== 情况3：单个更新或删除 ==========
        sysAquaticSpecies.setUpdateTime(now);
        sysAquaticSpecies.setUpdatedBy(username);
        return sysAquaticSpeciesMapper.updateSysAquaticSpecies(sysAquaticSpecies);
    }

    /**
     * 批量删除养殖生物信息
     * 
     * @param ids 需要删除的养殖生物信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAquaticSpeciesByIds(Long[] ids)
    {
        return sysAquaticSpeciesMapper.deleteSysAquaticSpeciesByIds(ids);
    }

    /**
     * 删除养殖生物信息信息
     * 
     * @param id 养殖生物信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAquaticSpeciesById(Long id)
    {
        return sysAquaticSpeciesMapper.deleteSysAquaticSpeciesById(id);
    }
}
